package com.yl.androidutilscenter.base.activity

import android.content.Intent
import android.os.Bundle
import android.os.Parcelable
import androidx.activity.result.ActivityResult
import androidx.activity.result.ActivityResultCallback
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.yl.androidutilscenter.dialog.LoadingDialog
import com.yl.androidutilscenter.base.BaseApplication
import com.yl.androidutilscenter.http.sendRequest
import com.yl.androidutilscenter.utils.ext.hideInput
import kotlinx.coroutines.launch
import java.io.Serializable

/**
 * @author
 * @description: BaseActivity
 * @date :2024/3/8
 * @version 1.0.0
 */
abstract class BaseActivity : AppCompatActivity() {
    private val mLoadingDialog by lazy { LoadingDialog(this, null) }
    private var customLoadingDialog: AlertDialog? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        BaseApplication.getInstance().addActivityToList(this)
    }

    override fun onDestroy() {
        super.onDestroy()
        if (customLoadingDialog == null) {
            mLoadingDialog.cancel()
        } else {
            customLoadingDialog?.cancel()
        }
        BaseApplication.getInstance().removeActivityFromList(this)
    }


    /**
     * 显示加载中的弹窗
     */
    open fun showLoading(tip: String? = null, loadingDialog: AlertDialog? = null) {
        hideInput()
        lifecycleScope.launch {
            if (loadingDialog == null) {
                mLoadingDialog.show()
                tip?.let { mLoadingDialog.setTip(it) }
                mLoadingDialog.startAnimation()
            } else {
                customLoadingDialog = loadingDialog
                customLoadingDialog?.show()
            }
        }
    }

    /**
     * 关闭弹窗
     */
    fun hideLoading() {
        if (customLoadingDialog == null) {
            mLoadingDialog.dismiss()
        } else {
            customLoadingDialog?.dismiss()
        }
    }

    /**
     * Activity中发送请求
     */
    open fun <T> sendRequest(
        requestBlock: suspend () -> T,
        errorCallBack: ((error: Throwable) -> Unit)? = null,
        callback: (T) -> Unit
    ) {
        this.lifecycleScope.sendRequest(requestBlock, errorCallBack, callback)
    }


    /**
     * 跳转activity，不传递数据
     */
    //@JvmOverloads
    fun jump2Activity(
        targetActivity: Class<*>,
        isClose: Boolean = false
    ) {
        val mIntent = Intent(this, targetActivity)
        startActivity(mIntent)
        if (isClose) {
            finish()
        }
    }


    //@JvmOverloads
    /**
     *
     */
    fun jump2Activity(
        targetActivity: Class<*>,
        value: Any?,
        key: String = "data",
        isClose: Boolean = false
    ) {
        val mIntent = Intent(this, targetActivity)
        if (value != null) {
            when (value) {
                is Int -> mIntent.putExtra(key, value)
                is Float -> mIntent.putExtra(key, value)
                is Double -> mIntent.putExtra(key, value)
                is String -> mIntent.putExtra(key, value)
                is Short -> mIntent.putExtra(key, value)
                is Long -> mIntent.putExtra(key, value)
                is Boolean -> mIntent.putExtra(key, value)
                is Parcelable -> mIntent.putExtra(key, value)
                is Serializable -> mIntent.putExtra(key, value)
            }
        }
        startActivity(mIntent)
        if (isClose) {
            finish()
        }
    }


    /**
     * 跳转activity，使用bundle携带数据，key = “data”
     */
    //@JvmOverloads
    fun jump2Activity(
        targetActivity: Class<*>,
        bundle: Bundle,
        key: String = "data",
        isClose: Boolean = false
    ) {
        val mIntent = Intent(this, targetActivity)
        mIntent.putExtra(key, bundle)
        startActivity(mIntent)
        if (isClose) {
            finish()
        }
    }

    /**
     * 使用ActivityLauncher 来跳转activity并获取返结果
     *  return ActivityResultLauncher<Intent>,  launcher.launch(intent)启动跳转
     *  @param resultCallBack  处理返回该页面时传递过来的数据 it.data
     */
    fun startActivityForResultFromLauncher(resultCallBack: ActivityResultCallback<ActivityResult>): ActivityResultLauncher<Intent> {
        return registerForActivityResult(
            ActivityResultContracts.StartActivityForResult(),
            resultCallBack
        )
    }



}